package org.example.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


//1. 把M4A转换成WAV
public class M4aToWav {

//    public static void main(String[] args) {
//        // 输入 M4A 文件路径
//        String inputFilePath = "D:\\heima-java\\ssm\\01-spring\\day02\\代码\\spring\\demo23232\\hm-dianping\\upload\\voice\\2024\\12\\06/a.m4a";  // 请替换为实际路径
//        // 输出 WAV 文件路径
//        String outputFilePath = "D:\\heima-java\\ssm\\01-spring\\day02\\代码\\spring\\demo23232\\hm-dianping\\upload\\voice\\2024\\12\\06/aw.wav";  // 请替换为实际路径
//
//        // 调用 FFmpeg 转换 M4A 为 WAV
//        try {
//            convertM4AToWav(inputFilePath, outputFilePath);
//            System.out.println("转换成功！输出文件: " + outputFilePath);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    public  void convertM4AToWav(String ffmpegPath,String inputFilePath, String outputFilePath) throws IOException {
        String ffmpegCommand = ffmpegPath+" -i \"" + inputFilePath + "\" \"" + outputFilePath + "\"";

        // 使用 ProcessBuilder 执行 FFmpeg 命令
        ProcessBuilder processBuilder = new ProcessBuilder(ffmpegCommand.split(" "));
        processBuilder.redirectErrorStream(true);  // 合并标准输出和错误输出
        Process process = processBuilder.start();

        // 读取 FFmpeg 命令输出
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);  // 打印 FFmpeg 输出
            }
        }

        // 等待命令执行完成
        try {
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new IOException("FFmpeg 命令执行失败，退出码: " + exitCode);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
